/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package GUI.Components;

import Tools.CMDThreadGUI;
import Tools.TModel;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Iterator;
import java.util.Map;
import javax.swing.JOptionPane;

/**
 *
 * @author Иван
 */
public class BrowserTree extends ABrowserBuilder {

    private Map<String, String> map;

    public BrowserTree(TModel model, AObserverGUI observerGUI) {
        super(model, observerGUI);
        setAction(action);
    }
    private MouseListener action = new MouseListener() {

        @Override
        public void mouseClicked(MouseEvent e) {
            Activate();
            if ((e.getClickCount() == 2)) {

                Activate();

                int i = browserList.getSelectedIndex();

                Iterator iterator = map.entrySet().iterator();
                boolean ok = true;
                while (ok && iterator.hasNext()) {
                    Map.Entry<String, String> entry = (Map.Entry<String, String>) iterator.next();
                    if (listModel.get(i).equals(entry.getValue())) {
                        setBrowserDirectory(entry.getKey());
                        ok = false;
                    }
                }

                hideBrowser();
                Disable();
                observerGUI.setCommandThread(new CMDThreadGUI("cd " + getBrowserDirectory(), observerGUI));



                try {
                    observerGUI.getCommandThread().t.join();
 
                } catch (InterruptedException ex) {
                    JOptionPane.showConfirmDialog(observerGUI.getGUI(), ex.getMessage(), "Error", JOptionPane.ERROR, JOptionPane.ERROR_MESSAGE);
                }

            }
        }

        @Override
        public void mousePressed(MouseEvent e) {
        }

        @Override
        public void mouseReleased(MouseEvent e) {
        }

        @Override
        public void mouseEntered(MouseEvent e) {
        }

        @Override
        public void mouseExited(MouseEvent e) {
        }
    };

    @Override
    public void updateComponent(Object parameters) {
        map = (Map<String, String>) parameters;

        listModel.clear();

        Iterator iterator = map.entrySet().iterator();

        while (iterator.hasNext()) {
            Map.Entry<String, String> entry = (Map.Entry<String, String>) iterator.next();

            listModel.addElement(entry.getValue());

        }

        browserList.setSelectedIndex(index);
        browserScrollPane.setViewportView(browserList);

    }
}
